package validator;

import entity.User;
import manager.UserManager;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class RegisterValidator implements Validator {

    private UserManager userManager;

    @Override
    public boolean supports(Class clazz) {
        //just validate the Customer instances
        return User.class.isAssignableFrom(clazz);

    }

    @Override
    public void validate(Object target, Errors errors) {

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
                "required.userName", "Field name is required.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address",
                "required.address", "Field name is required.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "pass",
                "required.password", "Field name is required.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPass",
                "required.confirmPassword", "Field name is required.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "sex",
                "required.sex", "Field name is required.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "age",
                "required.age", "Field name is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email",
                "required.email", "Field name is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "telephone",
                "required.telephone", "Field name is required.");


        User cust = (User) target;
//        User findUser = this.userManager.searchUser(cust.getName());
// 
//        User findUser = new User();
        if (!(cust.getPass().equals(cust.getConfirmPass()))) {
            errors.rejectValue("pass", "notmatch.password");
        }


    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
}